package com.sammy.appmonitor.storage;

import com.sammy.appmonitor.config.MyConstants;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class MySharedPreferences {
	
	//http://stackoverflow.com/questions/4693387/sharedpreferences-and-thread-safety
	//Processes and Threads are different. The SharedPreferences implementation in Android is thread-safe but not process-safe.
	
	public static final String KEY_DEVICES_OBJECT_ID = MyConstants.TN_DEVICES + "." + MyConstants.TF_DEVICES_OBJECT_ID;
	
	public static String getStringProperty(Context context, String key, String value) {
		SharedPreferences preferences = null;

		try {
			preferences = PreferenceManager.getDefaultSharedPreferences(context);
		} catch (Exception e) {			
			e.printStackTrace();
		}
		
		if (preferences != null) {
			return preferences.getString(key, value);
		}

		return value;
	}

	public static void setStringProperty(Context context, String value, String key) {
		SharedPreferences preferences = null;
		SharedPreferences.Editor editor = null;

		try {
			preferences = PreferenceManager.getDefaultSharedPreferences(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (preferences != null) {
			editor = preferences.edit();
			if (editor != null) {
				editor.putString(key, value);
				editor.commit();
			}
		}
	}
}
